package com.jy.adscm.operation.mscyb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqzzSetting;
import com.jy.adscm.operation.mscyb.bean.QueryJqbzSettings;
import com.jy.adscm.operation.mscyb.service.JqzzSettingService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-05-21
 */
@RestController
@RequestMapping("/adscm/operation/jqzz/jqzzSetting")
public class JqzzSettingController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    JqzzSettingService jqzzSettingService;

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody JqzzSetting jqzzSetting, HttpServletRequest request){
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqzzSetting.setCreator(token.getUser().getId().toString());
        jqzzSetting.setCreateTime(new Date());
        jqzzSetting.setIsEnable(1);
        boolean insert = jqzzSettingService.insert(jqzzSetting);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody JqzzSetting jqzzSetting, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqzzSetting.setCreator(token.getUser().getId().toString());
        jqzzSetting.setCreateTime(new Date());
        boolean result = jqzzSettingService.updateById(jqzzSetting);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectById/{id}")
    public  SimpleCoreResponse  selectByTid(@PathVariable String id) throws  AdscmSystemException{
        JqzzSetting jqzzSetting = jqzzSettingService.selectJqzzSettingById(id);
        return  new SimpleCoreResponse("200","查询成功",jqzzSetting);
    }
    @RequestMapping("selectAll")
    public  SimpleCoreResponse  selectAll(@RequestBody QueryJqbzSettings queryJqbzSettings, HttpServletRequest request) throws  AdscmSystemException{
        Page<JqzzSetting> page =new Page<>(queryJqbzSettings.getCurrentPage(),queryJqbzSettings.getPageSize());
        page =  jqzzSettingService.selectAll(page,queryJqbzSettings);
        return  new SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("select")
    public  SimpleCoreResponse  select( HttpServletRequest request) throws  AdscmSystemException{
        List<JqzzSetting> jqzzSettings = jqzzSettingService.selectList(null);
        return  new SimpleCoreResponse("200","查询成功",jqzzSettings);
    }
}

